[PATCH] [HIP] Detect HIP for Debian/Fedora
authorYaxun (Sam) Liu <yaxun.liu@amd.com>
Tue, 11 Oct 2022 22:53:59 +0000 (18:53 -0400)
committerGianfranco Costamagna <locutusofborg@debian.org>
Thu, 7 Sep 2023 22:43:45 +0000 (00:43 +0200)
HIP is installed at /usr or /usr/local on Debin/Fedora,
and the version file is at {root}/share/hip/version.

Reviewed by: Artem Belevich

Differential Revision: https://reviews.llvm.org/D135796

Gbp-Pq: Topic amdgpu
Gbp-Pq: Name usr-search-paths.patch

clang/lib/Driver/ToolChains/AMDGPU.cpp
clang/lib/Driver/ToolChains/ROCm.h
clang/test/Driver/hip-version.hip

index 8718ab53ac1a6e65864519ff195763e3f5fe1a2b..fc47d7f1994404e8fe264dd7c8e36b93072b2255 100644 (file)
 #include "CommonArgs.h"
 #include "clang/Basic/TargetID.h"
 #include "clang/Driver/Compilation.h"
+#include "clang/Driver/Distro.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/InputInfo.h"
 #include "clang/Driver/Options.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/LineIterator.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/VirtualFileSystem.h"
@@ -306,6 +308,14 @@ RocmInstallationDetector::getInstallationPathCandidates() {
     ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/" + LatestROCm,
                                 /*StrictChecking=*/true);
 
+  Distro Dist(D.getVFS(), llvm::Triple(llvm::sys::getProcessTriple()));
+  if (Dist.IsDebian() || Dist.IsRedhat()) {
+    ROCmSearchDirs.emplace_back(D.SysRoot + "/usr/local",
+                                /*StrictChecking=*/true);
+    ROCmSearchDirs.emplace_back(D.SysRoot + "/usr",
+                                /*StrictChecking=*/true);
+  }
+
   DoPrintROCmSearchDirs();
   return ROCmSearchDirs;
 }
@@ -461,18 +471,30 @@ void RocmInstallationDetector::detectHIPRuntime() {
     llvm::sys::path::append(IncludePath, "include");
     LibPath = InstallPath;
     llvm::sys::path::append(LibPath, "lib");
-
-    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
-        FS.getBufferForFile(BinPath + "/.hipVersion");
-    if (!VersionFile && Candidate.StrictChecking)
-      continue;
-
-    if (HIPVersionArg.empty() && VersionFile)
-      if (parseHIPVersionFile((*VersionFile)->getBuffer()))
+    SharePath = InstallPath;
+    llvm::sys::path::append(SharePath, "share");
+
+    // If HIP version file can be found and parsed, use HIP version from there.
+    for (const auto &VersionFilePath :
+         {std::string(SharePath) + "/hip/version",
+          std::string(BinPath) + "/.hipVersion"}) {
+      llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
+          FS.getBufferForFile(VersionFilePath);
+      if (!VersionFile)
         continue;
+      if (HIPVersionArg.empty() && VersionFile)
+        if (parseHIPVersionFile((*VersionFile)->getBuffer()))
+          continue;
 
-    HasHIPRuntime = true;
-    return;
+      HasHIPRuntime = true;
+      return;
+    }
+    // Otherwise, if -rocm-path is specified (no strict checking), use the
+    // default HIP version or specified by --hip-version.
+    if (!Candidate.StrictChecking) {
+      HasHIPRuntime = true;
+      return;
+    }
   }
   HasHIPRuntime = false;
 }
index 33baaa887043e798add8c7a26aefbb4342393512..b16deecdebec583b8c48825411b66d50e6f430fd 100644 (file)
@@ -107,6 +107,7 @@ private:
   SmallString<0> LibPath;
   SmallString<0> LibDevicePath;
   SmallString<0> IncludePath;
+  SmallString<0> SharePath;
   llvm::StringMap<std::string> LibDeviceMap;
 
   // Libraries that are always linked.
index 69995899fb2ac166b8ed37a6b91339ccf78c2af9..977bf959e29cef69c3f60e19049a1b0e39825c7f 100644 (file)
@@ -9,6 +9,14 @@
 // RUN:   -target amdgcn-amd-amdhsa \
 // RUN:   | FileCheck -check-prefixes=FOUND %s
 
+// RUN: rm -rf %t/Inputs
+// RUN: mkdir -p %t/Inputs
+// RUN: cp -r %S/Inputs/rocm %t/Inputs
+// RUN: mkdir -p %t/Inputs/rocm/share/hip
+// RUN: mv %t/Inputs/rocm/bin/.hipVersion %t/Inputs/rocm/share/hip/version
+// RUN: %clang -v --rocm-path=%t/Inputs/rocm 2>&1 \
+// RUN:   | FileCheck -check-prefixes=FOUND %s
+
 // FOUND: Found HIP installation: {{.*Inputs.*rocm}}, version 3.6.20214-a2917cd
 
 // When --rocm-path is set and .hipVersion is not found, use default version